package com.cz.nongyetongv5.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TouRuPinIconGrideAdapter extends BaseAdapter {

	private List<Map<String, Object>> iconGridData = null;

	private LayoutInflater layoutInflater = null;

	private OnClickListener onClickListener = null;

	private Context context = null;

	private List<View> views = new ArrayList<View>();

	public TouRuPinIconGrideAdapter(Context context,
			List<Map<String, Object>> iconGridData,
			LayoutInflater layoutInflater, OnClickListener onClickListener) {
		this.iconGridData = iconGridData;
		this.layoutInflater = layoutInflater;
		this.onClickListener = onClickListener;
		this.context = context;
	}

	@Override
	public int getCount() {
		return iconGridData.size();
	}

	@Override
	public Object getItem(int position) {
		return iconGridData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewGroup iconLayout = null;

		int ibIconItem = (Integer) iconGridData.get(position).get("ibIconItem");

		String iconText = (String) iconGridData.get(position).get("tvIconItem");

		if (convertView != null) {
			iconLayout = (ViewGroup) convertView;
		} else {
			iconLayout = (ViewGroup) layoutInflater.inflate(
					R.layout.gridview_item, null);
		}

		ImageView imageButton = (ImageView) iconLayout
				.findViewById(R.id.ibIconItem);

		imageButton.setBackgroundResource(ibIconItem);

		imageButton.setTag(position);

		imageButton.setOnClickListener(onClickListener);

		TextView textView = (TextView) iconLayout.findViewById(R.id.tvIconItem);

		textView.setText(iconText);

		return iconLayout;
	}

	@Override
	public void notifyDataSetChanged() {
		views.clear();

		super.notifyDataSetChanged();
	}

	public View getView(int position) {

		return views.get(position);
	}

}
